package com.example.common.resp;

import com.example.common.enums.ResultCode;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Schema(description = "全局返回体")
public class GlobalResult<T> {

    @Schema(description = "返回码")
    private int code;

    @Schema(description = "返回信息")
    private String msg;

    @Schema(description = "返回数据")
    private T data;

    public GlobalResult(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static <T> GlobalResult<T> success(String message, T data) {
        return new GlobalResult<>(ResultCode.SUCCESS.getCode(), message, data);
    }

    public static <T> GlobalResult<T> success(T data) {
        return new GlobalResult<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getDesc(), data);
    }

    public static <T> GlobalResult<T> success() {
        return new GlobalResult<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getDesc());
    }

    public static <T> GlobalResult<T> error(int code, String message) {
        return new GlobalResult<>(code, message);
    }
}
